/*
 * 标签, 一般用于显示字串, 没有事件, 也没有图片
 */

#include "gui.h"
#include <stdlib.h>
#include <string.h>

void ACALLBACK __label_render(struct Area *who,
			       void *userdata)
{
	struct Label *lb = (struct Label *) userdata;
	int x, y;
	x = who -> _x;
	y = who -> _y;
	
	if (lb -> _font && lb -> _caption) {
		Image a;
		a = ttf_make_img(lb -> _font, lb -> _caption, lb -> _font_color);
		int xx, yy;
		switch(lb -> _align) {
		case ALIGN_CENTER:
			xx = x + (who -> _width - img_get_width(a)) / 2;
			yy = y + (who -> _height - img_get_height(a)) / 2;
			break;
		case ALIGN_RIGHT:
			xx = x + (who -> _width - img_get_width(a));
			yy = y + (who -> _height - img_get_height(a));
			break;
		default:
			xx = x;
			yy = y;
			break;
		}
		video_drawimg(a, xx, yy);
		video_destory_img(a);
	}
}

struct Label *label_new(struct Panel *father, const char *cap, int x, int y,
			  int width, int height)
{
	struct Label *ret;
	ret = malloc(sizeof(struct Label));
	memset(ret, 0, sizeof(struct Label));
	
	struct Area *a;
	int xx, yy;
	if (father) {		//换成绝对坐标
		a = father -> _area;
		xx = father -> _area -> _x + x;
		yy = father -> _area -> _y + y;
	} else {
		a = NULL;
		xx = x;
		yy = y;
	}
	ret -> _area = area_new(a, xx, yy, width, height);
	ret -> _area -> enable = 1;
	ret -> _area -> visual = 1;
	ret -> _area -> userdata = ret;
	
	ret -> _area -> user_render = __label_render;
	
	if (cap)
		label_set_caption(ret, cap);
	
	ret -> _font = gui_get_font_small();
	return ret;
}

void label_set_caption(struct Label *lb, const char *cap)
{
	if (lb -> _caption) free(lb -> _caption);
	lb -> _caption = malloc(strlen(cap) + 2);
	strcpy(lb -> _caption, cap);
}

void label_set_font(struct Label *lb, Font font)
{
	lb -> _font = font;
}


void label_delete(struct Label *lb)
{
	lb -> _area -> enable = 0;
	lb -> _area -> visual = 0;
	if (lb -> _caption) free(lb -> _caption);
	free(lb);
}

void label_set_color(struct Label *lb, Uint32 color)
{
	lb -> _font_color = color;
}

void label_set_align(struct Label *lb, int align)
{
	lb -> _align = align;
}
